Skip to content

Conversation

@szydlovsky
Copy link
Contributor

Summary

As stated in #5826, layout animation that had a custom animation inside (specifically, withDelay that launched a withTiming that animates background colour change) just wasn't properly working. Turned out, that we have to pass values between colour animations as unprocessed rgba's rather than processed numbers.

This PR fixes it and, of course, the issue as well.

Test plan

You can try the following code (it is a cleaned version of snack given in the mentioned issue):

Code
import { Text, SafeAreaView, StyleSheet, View } from 'react-native';
import {
  Gesture,
  GestureDetector,
  GestureHandlerRootView,
} from 'react-native-gesture-handler';
import Animated, {
  runOnJS,
  withDelay,
  withTiming,
} from 'react-native-reanimated';
import React, { useState } from 'react';

export default function EmptyExample() {
  const [myArray, setArray] = useState([1, 2, 3, 4, 5]);

  const customAnim = () => {
    'worklet';
    const animations = {
      backgroundColor: withDelay(
        500,
        withTiming('rgba(179, 6, 6, 1)', { duration: 500 })
      ),
    };
    const initialValues = { backgroundColor: 'rgba(16, 128, 26, 1)' };
    return { initialValues, animations };
  };

  const handleAddElement = Gesture.Tap().onStart(() => {
    runOnJS(setArray)([...myArray, Math.random().toString()]);
  });
  return (
    <SafeAreaView style={styles.container}>
      <GestureHandlerRootView>
        <GestureDetector gesture={handleAddElement}>
          <View>
            <Text>Add Element</Text>
          </View>
        </GestureDetector>
        {myArray.map((item) => {
          return (
            <Animated.View key={item.toString()} entering={customAnim}>
              <Text>{item}</Text>
            </Animated.View>
          );
        })}
      </GestureHandlerRootView>
    </SafeAreaView>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    backgroundColor: '#ecf0f1',
    padding: 8,
  },
});

@szydlovsky szydlovsky requested a review from piaskowyk April 15, 2024 14:33
@tomekzaw
Copy link
Member

Looks good, let's add runtime tests for that (cc @Latropos)

@szydlovsky szydlovsky force-pushed the @szydlovsky/layout-animations-with-colors-fix branch from 0335fb7 to 2d6e8dc Compare April 19, 2024 12:22
@szydlovsky szydlovsky linked an issue Apr 22, 2024 that may be closed by this pull request
@szydlovsky szydlovsky requested a review from Latropos April 25, 2024 11:15
Copy link
Contributor

@Latropos Latropos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!
You may consider including transparency (or hex colors, or nnamed colors) in your tests as well

@Latropos
Copy link
Contributor

When adding snaphot to CSpell.json please try to keep these words in alphabetical order

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Custom Animation with "withDelay" cause strange behavior

3 participants